package itsm.isperp.framework.data.mybatis.plugin;

import itsm.isperp.framework.collection.tree.ITreeNode;
import itsm.isperp.framework.data.domain.DataResponse;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.web.request.JqGridRequest;

import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

/**
 * 一个JqGrid服务端相应实体。实现List接口{@link java.util.List}
 * 
 * @author lizx
 * @date 2013-1-17
 * @version V1.0
 */
public class JqGridResponseData<T> implements DataResponse<T>, Serializable {

	private static final long serialVersionUID = 1L;

	private List<T> content;
	private Pageable pageable;
	private long total;

	private ITreeNode[] menudata;

	public JqGridResponseData(List<T> content, Pageable pageable, long total) {
		if (null == content) {
			throw new IllegalArgumentException("Content must not be null!");
		}

		this.content = content;
		this.total = total;
		this.pageable = pageable;
	}

	public JqGridResponseData(List<T> content) {
		this(content, null, null == content ? 0 : content.size());
	}

	public void setResult(List<T> ret, JqGridRequest req, int total) {
		this.content = ret;
		this.total = total;
		this.pageable = req;
	}

	public JqGridResponseData() {
	}

	public ITreeNode[] getMenudata() {
		return menudata;
	}

	public void setMenudata(ITreeNode[] menudata) {
		this.menudata = menudata;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.data.domain.Page#getNumber()
	 */
	public int getNumber() {
		return pageable == null ? 0 : pageable.getPageNumber();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.data.domain.Page#getSize()
	 */
	public int getSize() {
		return pageable == null ? 0 : pageable.getPageSize();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.data.domain.Page#getTotalPages()
	 */
	public int getTotalPages() {
		return getSize() == 0 ? 1 : (int) Math.ceil((double) total
				/ (double) getSize());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.data.domain.Page#getNumberOfElements()
	 */
	public int getNumberOfElements() {
		return content.size();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.data.domain.Page#getTotalElements()
	 */
	public long getTotalElements() {
		return total;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.data.domain.Page#hasPreviousPage()
	 */
	public boolean hasPreviousPage() {
		return getNumber() > 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.data.domain.Page#isFirstPage()
	 */
	public boolean isFirstPage() {
		return !hasPreviousPage();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.data.domain.Page#hasNextPage()
	 */
	public boolean hasNextPage() {
		return getNumber() + 1 < getTotalPages();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.data.domain.Page#isLastPage()
	 */
	public boolean isLastPage() {
		return !hasNextPage();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.data.domain.Page#nextPageable()
	 */
	public Pageable nextPageable() {
		return hasNextPage() ? pageable.next() : null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.data.domain.Page#previousOrFirstPageable()
	 */
	public Pageable previousPageable() {

		if (hasPreviousPage()) {
			return pageable.previousOrFirst();
		}

		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.data.domain.Page#iterator()
	 */
	public Iterator<T> iterator() {
		return content.iterator();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.data.domain.Page#getContent()
	 */
	public List<T> getContent() {
		return content;
		// return Collections.unmodifiableList(content);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.data.domain.Page#hasContent()
	 */
	public boolean hasContent() {
		return !content.isEmpty();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.data.domain.Page#getSort()
	 */
	public Sort getSort() {
		return pageable == null ? null : pageable.getSort();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {

		String contentType = "UNKNOWN";

		if (content.size() > 0) {
			contentType = content.get(0).getClass().getName();
		}

		return String.format("Page %s of %d containing %s instances",
				getNumber(), getTotalPages(), contentType);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {

		if (this == obj) {
			return true;
		}

		if (!(obj instanceof PageImpl<?>)) {
			return false;
		}

		JqGridResponseData<?> that = (JqGridResponseData<?>) obj;

		boolean totalEqual = this.total == that.total;
		boolean contentEqual = this.content.equals(that.content);
		boolean pageableEqual = this.pageable == null ? that.pageable == null
				: this.pageable.equals(that.pageable);

		return totalEqual && contentEqual && pageableEqual;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {

		int result = 17;

		result = 31 * result + (int) (total ^ total >>> 32);
		result = 31 * result + (pageable == null ? 0 : pageable.hashCode());
		result = 31 * result + content.hashCode();

		return result;
	}

	@Override
	public boolean add(T e) {
		return content.add(e);
	}

	@Override
	public void add(int index, T element) {
		content.add(index, element);
	}

	@Override
	public boolean addAll(Collection<? extends T> c) {
		return content.addAll(c);
	}

	@Override
	public boolean addAll(int index, Collection<? extends T> c) {
		return content.addAll(index, c);
	}

	@Override
	public void clear() {
		content.clear();
	}

	@Override
	public boolean contains(Object o) {
		return content.contains(o);
	}

	@Override
	public boolean containsAll(Collection<?> c) {
		return content.containsAll(c);
	}

	@Override
	public T get(int index) {
		return content.get(index);
	}

	@Override
	public int indexOf(Object o) {
		return content.indexOf(o);
	}

	@Override
	public boolean isEmpty() {
		return content.isEmpty();
	}

	@Override
	public int lastIndexOf(Object o) {
		return content.lastIndexOf(o);
	}

	@Override
	public ListIterator<T> listIterator() {
		return content.listIterator();
	}

	@Override
	public ListIterator<T> listIterator(int index) {
		return content.listIterator(index);
	}

	@Override
	public boolean remove(Object o) {
		return content.remove(o);
	}

	@Override
	public T remove(int index) {
		return content.remove(index);
	}

	@Override
	public boolean removeAll(Collection<?> c) {
		return content.removeAll(c);
	}

	@Override
	public boolean retainAll(Collection<?> c) {
		return content.retainAll(c);
	}

	@Override
	public T set(int index, T element) {
		return content.set(index, element);
	}

	@Override
	public int size() {
		return content.size();
	}

	@Override
	public List<T> subList(int fromIndex, int toIndex) {
		return content.subList(fromIndex, toIndex);
	}

	@Override
	public Object[] toArray() {
		return content.toArray();
	}

	@Override
	public <T> T[] toArray(T[] a) {
		return content.toArray(a);
	}

	@Override
	public PageResponse<T> toPage() {
		return new PageResponse<>(this);
	}

	@Override
	public <S> Page<S> map(Converter<? super T, ? extends S> arg0) {
		return null;
	}

	@Override
	public boolean hasNext() {
		return false;
	}

	@Override
	public boolean hasPrevious() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isFirst() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isLast() {
		// TODO Auto-generated method stub
		return false;
	}

}